package com.sysadmin.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Vector;

import com.common.base.BaseService;
import com.common.jdbc.DB4MapUtil;
import com.common.jdbc.DB4ObjUtil;
import com.common.jdbc.DBSource;
import com.sysadmin.vo.MenusVO;
import com.sysadmin.vo.UsersVO;

public class LoginService extends BaseService {

	
	public boolean login(UsersVO vo){
		boolean isLogin = false;
		String sql = "select * from FXXT_SYS_USERS where 1=1 and USERNAME=? and PASSWORD=? and DEL_FLAG='0'";
		
		StringBuilder sqlM = new StringBuilder("select m.* from Fxxt_Sys_Roles r,Fxxt_Sys_Roletomenu rm,Fxxt_Sys_Menu m ");
		sqlM.append(" where r.id=rm.role_id and rm.menu_id=m.id and r.id=?");
		Connection conn = DBSource.getConnection();
		try{
		Vector<Hashtable> v = DB4MapUtil.execute4MapByConn(conn,sql, vo.getUsername(),vo.getPassword());
		if(v.size()>0){
			isLogin=true;
			vo.setUserFullname((String) v.get(0).get("USER_FULLNAME"));
			vo.setRoleId((String) v.get(0).get("ROLE_ID"));
			vo.setId((String) v.get(0).get("ID"));
			
			Vector<MenusVO> m = DB4ObjUtil.execute4ObjByConn(conn, new MenusVO(), sqlM.toString(), vo.getRoleId());
			vo.setMenusList(m);
		}
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return isLogin;
	}
}
